package fy.mdm.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;

import fy.mdm.bean.PurchasePartsBean;

public class PurchasePartsDao {
	private static Log log = LogFactory.getLog(PurchasePartsDao.class);
	private String[] ITEM;
	
	public JSONObject getPurchasePartsInfo() throws Exception {
		JSONObject jo = new JSONObject();
		for (int i = 0, length = this.ITEM.length; i < length; i++) {
			MeasurementUnitMapping mum = new MeasurementUnitMapping();
			PurchasePartsBean PurchasePartsBean=new PurchasePartsBean();
			IServiceRequester Requester = BaseServiceRequester.getInstance();
			// 定义请求报文对象
			IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
			String SourceSysID = "06004"; // 请求方(发布方)
			String ServiceID = "06001000000013";// 响应方(发布方)
			// 获取系统时间
			Date date = new Date();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyyMMddHHmmss");
			String format = simpleDateFormat.format(date);
			// 对请求报文对象进行设置
			reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
			reqMo.setSerialNO(format.substring(0, 8)
					+ SourceSysID
					+ String.format("%06d",
							++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
			reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
			reqMo.setServiceDateTime(format); // 设置报文 日期时间

			reqMo.setReqValue("MATERIEL_CODE", ITEM[i]);
			log.info("reqMo:" + reqMo);
			IMsgObject respMo = null;

			respMo = Requester.execute(reqMo);
			log.info("respMo:" + respMo);

			PurchasePartsBean.setITEM(ITEM[i]);

			List<GroupRecord> record2 = respMo.getResGroupRecord("ROW");
			for (GroupRecord groupRecord : record2) {
				PurchasePartsBean.setProjectDescription(groupRecord
						.getFieldValue("MATERIEL_DESCRIBE")); //材料名称项目说明
				PurchasePartsBean.setUnit(mum.getMeasurementUnit(
						"1", groupRecord.getFieldValue("MEASUREMENT"))); //单位

			}

			jo.put(PurchasePartsBean.getID(), PurchasePartsBean.getJSONObject());
		
		}
		log.info("jo:" + jo);
		return jo;
		
	}

	public String[] getITEM() {
		return ITEM;
	}

	public void setITEM(String[] iTEM) {
		ITEM = iTEM;
	}
	
}
